
/*
Copyright 2009 Peter Ekler, Akos Ludanyi, Budapest University of Technology and Economics, Department of Automation and Applied Informatics, Applied Mobile Research Group (AMORG) http://amorg.aut.bme.hu

This file is part of MobTorrent.

MobTorrent is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

MobTorrent is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with MobTorrent.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package data;

import Bencode.MTBencodedString;
import TorrentEngine.MTTorrent;
import TorrentEngine.MTTorrentManager;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import ui.MTUIManager;

/**
 *
 * @author Tyrael
 */
public class MTRMSManager
{

    public static void storeSettingsToRMS() throws Exception
    {
        // open data streams
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        DataOutputStream dout = new DataOutputStream(bout);
        RecordStore rs = null;
        // save settings to streams
        dout.writeInt(MTPreferences.LanguageCode);
        dout.writeUTF(MTPreferences.DownloadFolder);
        dout.writeBoolean(MTPreferences.EnableAddressBooster);
        dout.writeBoolean(MTPreferences.EnableStats);

        dout.flush();

        // store stream data to rms
        byte[] data = bout.toByteArray();
        rs = RecordStore.openRecordStore(MTPreferences.MTSETTINGSRMSNAME, true);
        try {
            rs.setRecord(1, data, 0, data.length);
        } catch (RecordStoreException ex) {
            rs.addRecord(data, 0, data.length);
        }

        dout.close();
        bout.close();
        rs.closeRecordStore();
    }

    public static void restoreSettingsFromRMS() throws Exception
    {
        RecordStore rs = null;
        // open RecordStore
        rs = RecordStore.openRecordStore(MTPreferences.MTSETTINGSRMSNAME, true);
        if (rs != null)
        {
            byte[] data = rs.getRecord(1);
            // create streams
            ByteArrayInputStream bin = new ByteArrayInputStream(data);
            DataInputStream din = new DataInputStream(bin);

            // read data
            MTPreferences.setLanguage(din.readInt());
            MTPreferences.DownloadFolder = din.readUTF();
            MTPreferences.EnableAddressBooster = din.readBoolean();
            MTPreferences.EnableStats = din.readBoolean();

            // close streams
            din.close();
            bin.close();

            // close rms
            rs.closeRecordStore();
        }
    }

    public static void storeTorrentsToRMS(MTTorrentManager aTorrentManager) throws Exception
    {
        // open data streams
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        DataOutputStream dout = new DataOutputStream(bout);
        RecordStore rs = null;
        dout.writeInt(aTorrentManager.getSimpleTorrentsNum());
        // save torrents
        for (int i=0; i<aTorrentManager.getSimpleTorrentsNum(); i++)
        {
            MTTorrent tempTorrent = aTorrentManager.simpleTorrentAtIndex(i);
            if (tempTorrent.isValid())
            {
                dout.writeUTF(tempTorrent.getTorrentFilePath());
                dout.writeUTF(tempTorrent.getTorrentNameWithoutPath());
                dout.writeUTF(tempTorrent.getDownloadFolder());
                dout.writeInt(tempTorrent.getBitField().data().length);
                dout.write(tempTorrent.getBitField().data());
            }
            else
            {
                dout.writeUTF("-");
            }
        }


        dout.flush();

        // store stream data to rms
        byte[] data = bout.toByteArray();
        rs = RecordStore.openRecordStore(MTPreferences.MTTORRENTSRMSNAME, true);
        try {
            rs.setRecord(1, data, 0, data.length);
        } catch (RecordStoreException ex) {
            rs.addRecord(data, 0, data.length);
        }

        dout.close();
        bout.close();
        rs.closeRecordStore();
    }

    public static void restoreTorrentsFromRMS(MTUIManager aUIManager) throws Exception
    {
        RecordStore rs = null;
        // open RecordStore
        rs = RecordStore.openRecordStore(MTPreferences.MTTORRENTSRMSNAME, true);
        if (rs != null)
        {
            byte[] data = rs.getRecord(1);
            // create streams
            ByteArrayInputStream bin = new ByteArrayInputStream(data);
            DataInputStream din = new DataInputStream(bin);

            // read torrents
            int storedTorrentsNum = din.readInt();

            for (int i=0;i<storedTorrentsNum;i++)
            {
                String torrentFilePath = din.readUTF();
                if (!torrentFilePath.equals("-"))
                {
                    String torrentNameWithoutPath = din.readUTF();
                    String downloadFolder = din.readUTF();
                    int bitFieldLength = din.readInt();
                    byte[] bitField = new byte[bitFieldLength];
                    din.read(bitField);

                    aUIManager.openNewTorrent(torrentFilePath, torrentNameWithoutPath,
                      true, bitField, downloadFolder);
                }
            }

            // close streams
            din.close();
            bin.close();

            // close rms
            rs.closeRecordStore();
        }
    }
}

